package com.example.programming.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.programming.common.Result;
import com.example.programming.sys.mapper.SchoolMapper;
import com.example.programming.sys.entity.School;

import org.springframework.web.bind.annotation.RestController;
import com.example.programming.common.tools.BaseController;

/**
 * <p>
 *  前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-06-27
 */
@RestController
@RequestMapping("/sys/school")
public class SchoolController extends BaseController {
    @Autowired
    private SchoolMapper schoolMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<School> page = new Page<School>(pageNum, pageSize);
        School parms = new School();
        QueryWrapper<School> warpper = new QueryWrapper<School>(parms);

        result.setData(schoolMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody School entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            schoolMapper.insert(entity);
        } else {
            schoolMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        schoolMapper.deleteBatchIds(deleteIds);
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/serch")
    public Result Serch(String school) {
        Result result = new Result();
        Map map = new HashMap<>();
        map.put("school", school);
        result.setData(schoolMapper.selectByMap(map));
        System.out.println(schoolMapper.selectByMap(map));
        return result;
    }

    @RequestMapping(method = RequestMethod.POST, value = "getcount")
    public Result getcount(String name) {
        Result result  = new Result();
        School parms = new School();
        QueryWrapper<School> warpper = new QueryWrapper<School>(parms);
        int count = schoolMapper.selectCount(warpper);
        result.setData(count);
        return result;
    }

    //顶层通知栏学校
    @RequestMapping(method= RequestMethod.POST,value="/schoolpage")
    public Result schoolpage(
            Integer pageNum,
            Integer pageSize,
            String name) {
        Result result = new Result();
        Page<School> page = new Page<School>(pageNum, pageSize);
        School parms = new School();
        QueryWrapper<School> warpper = new QueryWrapper<School>(parms);
        result.setData(schoolMapper.selectPage(page, warpper));
        return result;
    }

}
